package com.anasoft.os.m2st.doxia.macro;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.maven.doxia.macro.Macro;
import org.apache.maven.doxia.macro.MacroExecutionException;
import org.apache.maven.doxia.macro.MacroRequest;
import org.apache.maven.doxia.sink.Sink;

/**
 * {@link Macro} for inserting simple HTML tags into
 * the output document.
 * 
 * <p>
 * 
 * This macro takes exactly two parameters:
 * 
 * <ul>
 *  <li><tt>tag</tt> - tag name
 *  <li><tt>text</tt> - tag contents
 * </ul>
 * 
 * <p>
 * 
 * For example, macro definition with parameters
 * "h1" (<tt>tag</tt>) and "Hello!" (<tt>text</tt>)
 * results in
 * 
 * <pre>
 * &lt;h1&gt;Hello!&lt;/h1&gt;
 * </pre>
 * 
 * @see BaseMacro
 * 
 * @author vojtech.szocs
 * 
 * @plexus.component role="org.apache.maven.doxia.macro.Macro" role-hint="simple-tag"
 */
public class SimpleTagMacro extends BaseMacro {

    /**
     * @see org.apache.maven.doxia.macro.Macro#execute(org.apache.maven.doxia.sink.Sink, org.apache.maven.doxia.macro.MacroRequest)
     */
    public void execute(Sink sink, MacroRequest request) throws MacroExecutionException {
        String tag = getParameterValue(request, "tag", true);
        String text = getParameterValue(request, "text", true);
        
        StringBuilder html = new StringBuilder();
        html.append("<").append(tag).append(">");
        html.append(StringEscapeUtils.escapeHtml(text));
        html.append("</").append(tag).append(">");
        
        sink.rawText(html.toString());
    }
    
}
